function initArticleModel(sequelize, DataTypes) {
  const Article = sequelize.define(
    'Article', {
    'articleId': {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
      comment: '文章ID',
    },
    'title': {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: '文章标题',
    },
    'content': {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '文章内容',
    },
    'authorId': {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '作者ID',
    },
    'coverImage': {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '封面图片URL',
    },
    'categories': {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '文章分类',
      get() {
        const rawValue = this.getDataValue('categories');
        return rawValue ? JSON.parse(rawValue) : [];
      },
      set(value) {
        this.setDataValue('categories', JSON.stringify(value));
      }
    },
    'likeCount': {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '点赞数',
    },
    'favoriteCount': {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '收藏数',
    },
    'status': {
      type: DataTypes.ENUM('draft', 'pending', 'published'),
      allowNull: false,
      defaultValue: 'draft',
      comment: '文章状态',  // draft-草稿，pending-审核中，published-已发布
    },
    'publishTime': {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '发布时间',
    }
  }, {
    tableName: 'articles',
    indexes: [
      {
        name: 'idx_author',
        fields: ['authorId']
      },
      {
        name: 'idx_status',
        fields: ['status']
      }
    ]
  });

  // 类方法
  Article.findPopular = function(limit = 10) {
    return this.findAll({
      where: { status: 'published' },
      order: [['likeCount', 'DESC'], ['favoriteCount', 'DESC']],
      limit
    });
  };

  Article.findByAuthor = function(authorId) {
    return this.findAll({
      where: { authorId },
      order: [['createdAt', 'DESC']]
    });
  };

  // 定义关联关系
  Article.associate = function(models) {
    Article.belongsTo(models.User, {
      foreignKey: 'authorId',
      as: 'author'
    });
  };

  return Article;
}

module.exports = initArticleModel;
